package org.dromara.bzIns.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.bzIns.domain.bo.InsInsureBo;
import org.dromara.bzIns.domain.vo.InsInsureVo;
import org.dromara.bzIns.domain.InsInsure;
import org.dromara.bzIns.mapper.InsInsureMapper;
import org.dromara.bzIns.service.IInsInsureService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 保单记录Service业务层处理
 *
 * @author bz_huaYal9146
 * @date 2025-03-28
 */
@RequiredArgsConstructor
@Service
public class InsInsureServiceImpl implements IInsInsureService {

    private final InsInsureMapper baseMapper;

    /**
     * 查询保单记录
     *
     * @param insureId 主键
     * @return 保单记录
     */
    @Override
    public InsInsureVo queryById(Long insureId){
        return baseMapper.selectVoById(insureId);
    }

    /**
     * 分页查询保单记录列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 保单记录分页列表
     */
    @Override
    public TableDataInfo<InsInsureVo> queryPageList(InsInsureBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<InsInsure> lqw = buildQueryWrapper(bo);
        Page<InsInsureVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的保单记录列表
     *
     * @param bo 查询条件
     * @return 保单记录列表
     */
    @Override
    public List<InsInsureVo> queryList(InsInsureBo bo) {
        LambdaQueryWrapper<InsInsure> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    @Override
    public List<InsInsureVo> queryListWithCompany() {
        InsInsureBo insInsureBo = new InsInsureBo();
        //huaYal9146:伪代码 从会话中获取登录用户id
        insInsureBo.setUserId(1l);

        return baseMapper.queryListWithCompany(insInsureBo);
    }

    private LambdaQueryWrapper<InsInsure> buildQueryWrapper(InsInsureBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<InsInsure> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(InsInsure::getInsureId);
        lqw.eq(bo.getUserId() != null, InsInsure::getUserId, bo.getUserId());
        lqw.eq(bo.getCarId() != null, InsInsure::getCarId, bo.getCarId());
        lqw.eq(StringUtils.isNotBlank(bo.getCarPlate()), InsInsure::getCarPlate, bo.getCarPlate());
        lqw.like(StringUtils.isNotBlank(bo.getInsCompanyName()), InsInsure::getInsCompanyName, bo.getInsCompanyName());
        lqw.eq(bo.getInsuredTime() != null, InsInsure::getInsuredTime, bo.getInsuredTime());
        lqw.eq(bo.getGuaranteeStartTime() != null, InsInsure::getGuaranteeStartTime, bo.getGuaranteeStartTime());
        lqw.eq(bo.getExpirationTime() != null, InsInsure::getExpirationTime, bo.getExpirationTime());
        lqw.eq(bo.getIsDisplay() != null, InsInsure::getIsDisplay, bo.getIsDisplay());
        return lqw;
    }

    /**
     * 新增保单记录
     *
     * @param bo 保单记录
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(InsInsureBo bo) {
        InsInsure add = MapstructUtils.convert(bo, InsInsure.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setInsureId(add.getInsureId());
        }
        return flag;
    }

    /**
     * 修改保单记录
     *
     * @param bo 保单记录
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(InsInsureBo bo) {
        InsInsure update = MapstructUtils.convert(bo, InsInsure.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(InsInsure entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除保单记录信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
